package edu.scau.divineplace.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import edu.scau.divineplace.entity.dos.CommentScoreDO;
import edu.scau.divineplace.entity.dos.ContentBasicInformationDO;
import edu.scau.divineplace.entity.dos.UserDO;
import edu.scau.divineplace.entity.vo.CommentScoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: x1aolone
 * @Date: 2020/12/11 15:48
 */
@Service
public class CommentScoreService {

    @Autowired
    private ImportMapperService importMapperService;


    /**
     * 通过用户id获取他打的评分，支持完美分页
     * @param userId
     * @param currentPage
     * @param pageSize
     * @return
     */
    public List<CommentScoreVO> list(Long userId, Integer currentPage, Integer pageSize){
        if (currentPage == null) {
            currentPage = -1;
        }
        if (pageSize == null) {
            pageSize = -1;
        }
        List<CommentScoreVO> VOS= Lists.newArrayList();
        Page<CommentScoreDO> commentScorePage = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<CommentScoreDO> query = Wrappers.<CommentScoreDO>lambdaQuery()
                .eq(CommentScoreDO::getUserId, userId)
                .orderByDesc(CommentScoreDO::getUpdateTime)
                .orderByDesc(CommentScoreDO::getCreateTime);
        commentScorePage = importMapperService.commentScoreMapper.selectPage(commentScorePage, query);
        for (CommentScoreDO DO : commentScorePage.getRecords()) {
            ContentBasicInformationDO contentDO = importMapperService.contentBasicInformationMapper.selectById(DO.getContentId());
            VOS.add(new CommentScoreVO(DO, contentDO));
        }
        return VOS;
    }

}
